package com.huitone.gddw.service.impl.worktable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.InputLine;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IInputLineDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IInputLineService;
import com.huitone.gddw.service.dto.InputLineDTO;
import com.huitone.gddw.service.dto.business.InputWebSmDTO;
import com.huitone.gddw.service.system.IRoleService;
import com.huitone.gddw.utils.SpringContextUtil;
import com.huitone.gddw.utils.StringUtils;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 文件导入明细 接口实现类
 * @author wuluodan
 *
 */
@Service
public class InputLineService implements IInputLineService {

	@Autowired
	private IInputLineDao inputLineDao;
	
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private SpringContextUtil springContextUtil;
	
	@Override
	public QueryPage<InputLineDTO> selectPage(QueryPage<InputLine> page) {
		List<InputLineDTO> result = null;
		int totalCount = 0;
		
		Map<String, Object> map = page.getParams();
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		map.put("userId", SysLoginUtil.getLoginUserId());
		
		if ((page.getParams().get("fileType") != null && !"".equals(page.getParams().get("fileType").toString()) 
				&& page.getParams().get("fileType").toString().equals(ComConstant.ERROR_MNG)) 
				|| roleService.isRootAcsFlag(SysLoginUtil.getLoginUserId()) 
				) {
			result = inputLineDao.selectPage(map);
			totalCount = inputLineDao.selectCount(map);
		} else {
			result = inputLineDao.selectPageUnit(map);
			totalCount = inputLineDao.selectCountUnit(map);
		}
		
		QueryPage<InputLineDTO> queryPage = new QueryPage<InputLineDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public List<InputLineDTO> getRecord(Map<String, Object> params) {
		List<InputLineDTO> list = inputLineDao.getRecord(params);
		return list;
	}

	@SuppressWarnings("static-access")
	@Override
	public void saveBatch(List<InputLine> list) {
		String userId = SysLoginUtil.getLoginUserId();
		for (InputLine inputLine : list) {
			inputLine.setRecId(StringUtils.uuid());
			inputLine.setInsertTime(new Date());
			inputLine.setInsertStaffId(userId);
			inputLine.setLastModTime(inputLine.getInsertTime());
			inputLine.setLastModStaffId(inputLine.getInsertStaffId());
		}
		
		List<InputLine> li = new ArrayList<InputLine>();
		li.addAll(list);
		final IInputLineService inputLineServiceI = (IInputLineService)springContextUtil.getBean("inputLineService");
		while (list.size() != 0) {
			int size = 1000;
			int index = 0;
			if (list.size() < 1000) {
				size = list.size();
			}
			final List<InputLine> temp = new ArrayList<InputLine>();
			while (index < size) {
				temp.add(list.get(0));
				list.remove(0);
				index++;
			}

			new Thread() {
				public void run() {
					inputLineServiceI.insertbatch(temp);
				}
			}.start();
		}
		list.addAll(li);
	}
	
	@Override
	public void insertbatch(List<InputLine> list) {
		inputLineDao.insertbatch(list);
	}

	@Override
	public void updateBatch(List<InputLine> list) {
		if (list.isEmpty() || 0 == list.size()) {
			return;
		}
		while (list.size() != 0) {
			int size = 1000;
			int index = 0;
			if (list.size() < 1000) {
				size = list.size();
			}
			List<InputLine> temp = new ArrayList<InputLine>();
			while (index < size) {
				temp.add(list.get(0));
				list.remove(0);
				index++;
			}
			
			inputLineDao.updateBatch(temp);
		}
	}

	@Override
	public List<InputWebSmDTO> getRecordWebSm(Map<String, Object> params) {
		params.put("userId", SysLoginUtil.getLoginUserId());
		if (roleService.isRootAcsFlag(SysLoginUtil.getLoginUserId())) {
			return inputLineDao.getRecordWebSm(params);
		}
		return inputLineDao.getRecordWebSmUnit(params);
	}

	@Override
	public void delete(Map<String, Object> params) {
		inputLineDao.delete(params);
	}

}
